<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<base href="<%=basePath%>"/>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>人员表单</title>
	<link rel="stylesheet" href="res/layui/css/layui.css">
		<script src="res/js/jquery-2.1.4.min.js" type="text/javascript"></script>
		<script src="res/layui/layui.js"></script>
	</head>
	<body style="padding: 40px 40px 0px 40px">
		<form class="layui-form" id="staff">
			<input type="hidden" id="staffId" name="staffId">
			<div class="layui-form-item">
				<label class="layui-form-label">选择分店</label>
				<div class="layui-input-block">
					<select id="annexId" name="annexId" lay-verify="required" class="layui-input">
						
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">选择部门</label>
				<div class="layui-input-block">
					<select id="departId" name="departId" lay-verify="required" class="layui-input">
						
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">真实姓名</label>
				<div class="layui-input-block">
					<input type="text" id="staffName" name="staffName" lay-verify="required"
						placeholder="请输入真实姓名" autocomplete="off" class="layui-input">
				</div>
			</div>
	
			<div class="layui-form-item">
				<label class="layui-form-label">登录账号</label>
				<div class="layui-input-block">
					<input type="text" id="staffAccount" name="staffAccount" lay-verify="required"
						placeholder="请输入登录账号" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">登录密码</label>
				<div class="layui-input-block">
					<input type="text" id="staffPwd" name="staffPwd" lay-verify="required"
						placeholder="请输入登录密码" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">职位</label>
				<div class="layui-input-block">
					<input type="text" id="staffPost" name="staffPost" lay-verify="required"
						placeholder="请输入职位" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">地址</label>
				<div class="layui-input-block">
					<input type="text" id="staffDizhi" name="staffDizhi" lay-verify="required"
						placeholder="请输入地址" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">手机</label>
				<div class="layui-input-block">
					<input type="text" id="staffPhone" name="staffPhone" lay-verify="required"
						placeholder="请输入手机" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-form-item layui-form-text">
				    <label class="layui-form-label">备注</label>
				    <div class="layui-input-block">
				      <textarea placeholder="请输入内容" class="layui-textarea" id="staffRemar" name="staffRemar"></textarea>
				    </div>
				</div>
			</div>
			<input type="hidden" id="departBy" name="departBy" value="${staff.staffName }">
			<div class="layui-input-block">
		        <button class="layui-btn" lay-submit lay-filter="demo">立即提交</button>
		        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
		    </div>
		</form>
		
		<script type="text/javascript">
			var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
			
			var staffId;
			var annexId;
			var departId;
			
			$(function (){
				staffId = GetQueryString("staffId");
				if(staffId!=null){
					findById(staffId);
				}
			});
			
			var form;
			layui.use(['form'], function() {
				form = layui.form;
				//监听提交
				showSelect();
				form.on('submit(demo)', function(data){
					var data = $("#staff").serialize();
					var url = "staff/addOrUpdate.action";
					var loadIndex = layer.load();
					$.post(url, data, function(mes){
						parent.layer.msg(mes.mes);
						
						layer.close(loadIndex);//加载层关闭  
						parent.layer.close(index);
						if($("#staffId").val()!=""){
							$(".layui-laypage-skip .layui-laypage-btn",window.parent.document).click();//刷新父页面数据表格的当前页
						}else{
							parent.table.reload('staffId');
						}
					});
					return false;
				});
				
			});
			
			function findById(staffId){
				$.ajax({
					type: "POST",
					url: "staff/findById.action",
					data: {"staffId":staffId},
					async: false,
					error: function(){
						layer.mes("发生未知错误，请稍后再试！");
					},
					success: function(obj){
						departId = obj.departId;
						annexId = obj.annexId;
						
						$("#staffId").val(obj.staffId);
						$("#staffAccount").val(obj.staffAccount);
						$("#staffDizhi").val(obj.staffDizhi);
						$("#staffName").val(obj.staffName);
						$("#staffPhone").val(obj.staffPhone);
						$("#staffPost").val(obj.staffPost);
						$("#staffPwd").val(obj.staffPwd);
						$("#staffRemar").val(obj.staffRemar);
						
					}
				});
			}
			
			/**
			显示查询栏的部门和分店的下拉框
		*/
		function showSelect(){
			$("#departId").html('<option value=""></option>');
			$("#annexId").html('<option value=""></option>');
			$.ajax({
				type : "POST",
				url : "department/showList.action",
				data : {"page":1,"limit":9999}, 
				async : false,
				error : function(){
					layer.msg("发生未知错误，请稍后重试");
				},
				success : function(map){
					var data = map.data;
					for(i=0; i<data.length; i++){
						var obj = data[i];
						if(obj.departId==departId){
							$("#departId").append('<option value="'+obj.departId+'"  selected="selected" >'+obj.departName+'</option>');
						}else{ 
							$("#departId").append('<option value="'+obj.departId+'">'+obj.departName+'</option>');
						}
					}
				}
			});
			$.ajax({
				type : "POST",
				url : "annex/annexAjax.action",
				data : {"page":1,"limit":9999}, 
				async : false,
				error : function(){
					layer.msg("发生未知错误，请稍后重试");
				},
				success : function(map){
					var data = map.data;
					for(i=0; i<data.length; i++){
						var obj = data[i];
						if(obj.annexId==annexId){
							$("#annexId").append('<option value="'+obj.annexId+'" selected="selected" >'+obj.annexName+'</option>');
						}else{
							$("#annexId").append('<option value="'+obj.annexId+'">'+obj.annexName+'</option>');
						}
					}
				}
			});
			
			form.render();
		}
			
			/**
			得到URL栏的参数
			*/
			function GetQueryString(name){
			     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			     var r = window.location.search.substr(1).match(reg);
			     if(r!=null)return  unescape(r[2]); return null;
			}
		</script>
	</body>
</html>